package dd.control;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import xml.Message;
import dd.model.Event;
import dd.model.Model;
import dd.view.SignInScreen;
import dd.view.addChoiceScreen;
import dd.view.addEdgeFrame;

public class userSignInResponseController {
	/**
	 * call when get usersigninresponse
	 * the user get this information successful means he has the ability to sign in this event
	 * read all the information in the message and put it into local event
	 * go to different screen for different mode
	 */
	SignInScreen signinscreen;
	public userSignInResponseController(){
		Model model = Model.getInstance();
		signinscreen = model.getSignInScreen();
	}
	public void process(Message response){
		//initialize model
		Model model = Model.getInstance();
		Event e = model.getEvent();
		//get the event information
		Node update = response.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();
		String thisid = map.getNamedItem("id").getNodeValue();
		System.out.println("id:" + thisid);
		e.setEventID(thisid);
		String thismode = map.getNamedItem("type").getNodeValue();
		System.out.println("mode: " + thismode);
		e.setMode(thismode);
		String thistype = map.getNamedItem("behavior").getNodeValue();
		System.out.println("type: " + thistype);
		e.setType(thistype);
		String thisname = map.getNamedItem("question").getNodeValue();
		System.out.println("name: "+ thisname);
		e.setEventName(thisname);
		String thisnumoflines = map.getNamedItem("numChoices").getNodeValue();
		int thisnumofline = Integer.parseInt(thisnumoflines);
		System.out.println("number of choices: "+thisnumoflines);
		e.setNumberLines(thisnumofline);
		String thisnumofedges = map.getNamedItem("numRounds").getNodeValue();
		int thisnumofedge = Integer.parseInt(thisnumofedges);
		System.out.println("number of Round: " + thisnumofedges);
		e.setNumberEdges(thisnumofedge);
		e.setLeftEdges();
		String thispostion = map.getNamedItem("position").getNodeValue();
		int position = Integer.parseInt(thispostion);
		System.out.println("this user's position is: " +thispostion);
		e.setPostion(position);
		//get to first child---choice
		update = update.getFirstChild();
		map = update.getAttributes();
		String choice = null;
	//	int index = 0;
		int i;
		//get the choice information
		choice = map.getNamedItem("value").getNodeValue();
		System.out.println("choice0 is:"+ choice);
		e.setChoice(0, choice);
		for(i=1;i<thisnumofline;i++){
			update = update.getNextSibling();
			map = update.getAttributes();
			choice = map.getNamedItem("value").getNodeValue();
			System.out.println("choice"+i+" is:"+ choice);
			e.setChoice(i, choice);
		}
		//if this is in open mode, go to the set choice screen
		if(thismode.equals("open")){
			addChoiceScreen acs = new addChoiceScreen(e);
			acs.setVisible(true);
			signinscreen.dispose();
		}
		//if this in close mode, go the the add edge screen
		else if(thismode.equals("closed")){
			addEdgeFrame aef = new addEdgeFrame();
			aef.setVisible(true);
			signinscreen.dispose();
		}
		//save the client id

	}
}
